<?php

/**
 * +------ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER * ---------+
 * +----------------------------------------------------------------------------+
 * | PHPKiisf - Keep It in Simple Framework										|
 * | Version 1.0																|
 * | Copyright (C) <2013>  Mario Cezzare <mcezzare@gmail.com>					|
 * | Blog: http://mcezzare.blogspot.com.br										|
 * |																			|
 * | This program is part of PHPKiisf, and is free software: you can			|
 * | redistribute it and/or modify it under the terms of the GNU 				|
 * | General Public License as published by										|
 * | the Free Software Foundation, either version 3 of the License, or			|
 * | (at your option) any later version.										|
 * |																			|
 * | This program is distributed in the hope that it will be useful,			|
 * | but WITHOUT ANY WARRANTY; without even the implied warranty of				|
 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the				|
 * | GNU General Public License for more details.								|
 * | You should have received a copy of the GNU General Public License			|
 * | along with this program.  If not, see <http://www.gnu.org/licenses/>.		|
 * +----------------------------------------------------------------------------+
 * | Author : <Mario Cezzare Angelicola Chiodi> <mcezzare@gmail.com>			|
 * +----------------------------------------------------------------------------+
 * | Contributors																|
 * |																			|
 * +----------------------------------------------------------------------------+
 *
 */
/**
 * @package	Lib
 * @access	public
 * @since	2013-03-08
 * @author	Mario Cezzare <mcezzare@gmail.com>
 */

/**
 * Class to maintain the connections to database
 * @todo create pgsql and oracle drivers
 */
class connection extends default_controller {
		/* config access */

		private $host;
		private $user;
		private $pass;
		private $dbname;
		private $db;

		/* control */
		private $db_type;
		private $data;
		public $status;
		private $output;
		private $command;
		private $drivers = array(
				"MYSQL" => "mysql_driver" ,
				"POSTRESQL" => "pgsql_driver" ,
				"ORACLE" => "oracle_driver" ,
		);
		public $error	 = null;

		/**
		 *
		 */
		public static function init() {

		}

		/**
		 * Automagic
		 *
		 * @param type $db_type
		 * @todo melhorar o esquema do driver
		 */
		public function __construct( $db_type = "MYSQL" ) {
				if ( array_key_exists( $db_type , $this->drivers ) ) {
						$this->db_type = $db_type;
						$this->connect();
				}
				else {
						throw new Exception( "DB DRIVER NOT SUPORTED:" . $db_type , 13 );
				}
		}

		/**
		 * Disconnect from db server
		 */
		public function __destruct() {
				if ( $this->status == 1 ) {
						@$this->disconnect();
				}
		}

		/**
		 * Connect to DB
		 *
		 * @return boolean
		 */
		public function connect( $verbose = true ) {
				$this->status	 = 0;
				$this->host		 = DB_HOST;
				$this->user		 = DB_USER;
				$this->pass		 = DB_PASS;
				$this->dbname	 = DB_NAME;

				if ( $this->db_type == "MYSQL" ) {
						$this->db = @mysql_connect( $this->host , $this->user , $this->pass );

						if ( !$this->db ) {
//								display_message( sprintf( "Error connecting DB: %s" , mysql_error() ) ) ;
//								$this->error = mysql_error();
								$message	 = new message();
								$message	 = utils::prepare_message(
												array(
														"MSG_TYPE" => "ERROR" ,
														"OPERATION" => "CONNECT DB" ,
														"DETAIL" => "Can't connect DB." ,
														"OBS" => mysql_error() ,
												)
								);
								$this->error = $message;
//								utils::inform_user( $message );
								return false;
						}
						else {
								//echo "Connected";
								$this->status = 1;
								mysql_select_db( $this->dbname , $this->db );
								mysql_set_charset( 'utf8' );
								return true;
						}
				}
				// no driver
				return false;
		}

		/**
		 *
		 * @param type $query
		 * @return boolean
		 */
		public function execute_query( $query ) {

				if ( $this->status == 1 ) {
						if ( $this->db_type == "MYSQL" ) {

								if ( $this->output = mysql_query( $query ) ) {
										return $this->output;
								}
								else {
										$this->display_error();
//										$this->disconnect();
										return false;
								}
						}
				}
				else {
						utils::inform_user( $this->error );
				}
				// no driver
				return false;
		}

		/**
		 *
		 * @param type $query
		 * @return boolean
		 */
		public function execute_update( $query ) {
				if ( $this->status == 1 ) {
						if ( $this->db_type == "MYSQL" ) {
								if ( $this->command = mysql_query( $query ) ) {
										return true;
								}
								else {
										$this->display_error();
//										$this->disconnect();
										return false;
								}
						}
				}
				// no driver
				return false;
		}

		/**
		 *
		 * @return type
		 */
		private function disconnect() {
				if ( $this->status == 1 ) {
						$this->status = 0;
						return mysql_close( $this->db );
				}
		}

		/**
		 *
		 */
		public function display_error() {
				if ( $this->db_type == "MYSQL" ) {
						$s = "<pre class=\"error\">";
						$s.= "SQL ERROR: %s";
						$s.= "</pre>";
						printf( $s , mysql_error() );
				}
		}

}

?>